package com.corpdata.app.project.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.corpdata.core.base.AbstractBaseService;
import com.alibaba.fastjson.JSON;
import com.corpdata.app.customer.dao.CustomerIntegralMapper;
import com.corpdata.app.customer.entity.CustomerIntegral;
import com.corpdata.app.customer.entity.CustomerTeam;
import com.corpdata.app.customer.service.CustomerIntegralService;
import com.corpdata.app.project.dao.ProjectFieldMapper;
import com.corpdata.app.project.dao.ProjectFieldTeamMapper;
import com.corpdata.app.project.entity.ProjectField;
import com.corpdata.app.project.entity.ProjectFieldTeam;
import com.corpdata.app.project.service.ProjectFieldTeamService;
import com.corpdata.common.domain.DataGridRequestDTO;
import com.corpdata.common.result.Result;

/**
 * 预约场地关联表
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-18 16:28:24
 */
@Service
@Transactional
public class ProjectFieldTeamServiceImpl extends AbstractBaseService<ProjectFieldTeam> implements ProjectFieldTeamService {
	
	@Autowired
	private ProjectFieldTeamMapper projectFieldTeamMapper;
	
	@Autowired
	private CustomerIntegralService customerIntegralService;
	
	@Autowired
	private ProjectFieldMapper projectFieldMapper;
	
	public Result insert(ProjectFieldTeam record,String fieldId,String teamId) {
		ProjectField pf=new ProjectField();
		pf.setId(fieldId);
		record.setField(pf);
		CustomerTeam team=new CustomerTeam();
		team.setId(teamId);
		record.setTeam(team);
		
		CustomerIntegral ci=new CustomerIntegral();
		int checkPrice=0;
		ProjectField field=projectFieldMapper.selectById(fieldId);
		if(field.getCheckPrice()!=null){
			checkPrice=field.getCheckPrice().intValue();
		}
		if(checkPrice!=0){
			ci.setGiftRecharge(checkPrice);
		}else{
			ci.setGiftRecharge(0);
		}
		ci.setType(3);
		ci.setFkTeam(teamId);
		customerIntegralService.orderInsert(ci);
		
		return super.save(record);
	}
	
	public String selFieldTeam(String id){
		return JSON.toJSONStringWithDateFormat(super.findById(id),"yyyy-MM-dd");
	}
	
	public int selectFieldCount(DataGridRequestDTO dgRequest){
		return projectFieldTeamMapper.selectFieldCount(dgRequest.getParams());
	}
	public String selStatistics(DataGridRequestDTO dgRequest) throws ParseException{
		String json="";
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		int Number=0;
		double time=0;
		//一天的毫秒数
		long d = 1000 * 24 * 60 * 60;
		//一小时的毫秒数
		long h = 1000 * 60 * 60;
		List<ProjectFieldTeam> ptList=projectFieldTeamMapper.selectAll(dgRequest.getParams());
		for(ProjectFieldTeam pft:ptList){
			
			long a =formatter.parse(pft.getEndTime()+":00").getTime() - formatter.parse(pft.getStartTime()+":00").getTime();
			
			time+=a/h;
			Number++;
		}
		
		json="{\"Number\":\""+Number+"\",\"time\":\""+time+"\"}";
		return json;
	}
}
